package model

import (
	"database/sql"
	"time"
	"wxpay/pkg/util"

	"gorm.io/datatypes"
)

/*
DB Table Details
-------------------------------------


Table: profitreturn
[ 0] id                                             INT8                 null: false  primary: true   isArray: false  auto: false  col: INT8            len: -1      default: []
[ 1] brand_id                                       VARCHAR(16)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 16      default: []
[ 2] mch_id                                         VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
[ 3] sub_mch_id                                     VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
[ 4] appid                                          VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
[ 5] out_order_no                                   VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
[ 6] out_return_no                                  VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
[ 7] wx_order_id                                    VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
[ 8] return_mch_id                                  VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
[ 9] amount                                         INT8                 null: false  primary: false  isArray: false  auto: false  col: INT8            len: -1      default: []
[10] description                                    VARCHAR(80)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 80      default: []
[11] wx_return_no                                   VARCHAR(64)          null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
[12] result                                         VARCHAR(255)         null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 255     default: []
[13] fail_reason                                    VARCHAR(255)         null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 255     default: []
[14] extra                                          JSONB                null: true   primary: false  isArray: false  auto: false  col: JSONB           len: -1      default: []
[15] finish_time                                    TIMESTAMPTZ          null: true   primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []
[16] create_time                                    TIMESTAMPTZ          null: false  primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []
[17] update_time                                    TIMESTAMPTZ          null: false  primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []


JSON Sample
-------------------------------------
{    "out_order_no": "nUAZSqqQbwxNoXlnKpZOexkIK",    "fail_reason": "liVyaXgAdFSMkPxSldhpJkKxU",    "create_time": "2106-09-30T03:21:02.141960704+08:00",    "id": 2,    "appid": "eewXYmcTOYgMSoxyLfjSjBoPa",    "finish_time": "2241-12-04T05:32:23.154119898+08:00",    "wx_order_id": "uLcTXTJqYcOvoLcfeCxkFfVCA",    "description": "JToulmQJFQBxHHhjKZQlSjMNf",    "result": "qQMRONSlGvSkGqJoHhfiZIYXe",    "update_time": "2266-05-13T06:21:55.957374874+08:00",    "mch_id": "fUnxaulyfYVcogOmXHRtwhBgc",    "out_return_no": "sEgLZdxffDGLIEdGQjxAhdHCV",    "return_mch_id": "yJJKFIvTEHfyYaVVEoOuLGLWq",    "amount": 81,    "wx_return_no": "XibsxyTZLitDSOptvIPnMmCtG",    "extra": "GRiqTbbMRvGrBrTjKaRaZGEAK",    "brand_id": "kLaVPOYiFGNJhlOOjwkFiKlyR",    "sub_mch_id": "FypWyPlMRRmDMCRZmWOqYBaay"}



*/

// Profitreturn struct is a row record of the profitreturn table in the wxpay database
type Profitreturn struct {
	//[ 0] id                                             INT8                 null: false  primary: true   isArray: false  auto: false  col: INT8            len: -1      default: []
	ID int64 `gorm:"primary_key;column:id;type:INT8;"`
	//[ 1] brand_id                                       VARCHAR(16)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 16      default: []
	BrandID string `gorm:"column:brand_id;type:VARCHAR;size:16;"`
	//[ 2] mch_id                                         VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
	MchID string `gorm:"column:mch_id;type:VARCHAR;size:32;"`
	//[ 3] sub_mch_id                                     VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
	SubMchID string `gorm:"column:sub_mch_id;type:VARCHAR;size:32;"`
	//[ 4] appid                                          VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
	AppID string `gorm:"column:appid;type:VARCHAR;size:32;"`
	//[ 5] out_order_no                                   VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
	OutOrderNo string `gorm:"column:out_order_no;type:VARCHAR;size:64;"`
	//[ 6] out_return_no                                  VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
	OutReturnNo string `gorm:"column:out_return_no;type:VARCHAR;size:64;"`
	//[ 7] wx_order_id                                    VARCHAR(64)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
	WxOrderID string `gorm:"column:wx_order_id;type:VARCHAR;size:64;"`
	//[ 8] return_mch_id                                  VARCHAR(32)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 32      default: []
	ReturnMchID string `gorm:"column:return_mch_id;type:VARCHAR;size:32;"`
	//[ 9] amount                                         INT8                 null: false  primary: false  isArray: false  auto: false  col: INT8            len: -1      default: []
	Amount int64 `gorm:"column:amount;type:INT8;"`
	//[10] description                                    VARCHAR(80)          null: false  primary: false  isArray: false  auto: false  col: VARCHAR         len: 80      default: []
	Description string `gorm:"column:description;type:VARCHAR;size:80;"`
	//[11] wx_return_no                                   VARCHAR(64)          null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 64      default: []
	WxReturnNo sql.NullString `gorm:"column:wx_return_no;type:VARCHAR;size:64;"`
	//[12] result                                         VARCHAR(255)         null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 255     default: []
	Result sql.NullString `gorm:"column:result;type:VARCHAR;size:255;"`
	//[13] fail_reason                                    VARCHAR(255)         null: true   primary: false  isArray: false  auto: false  col: VARCHAR         len: 255     default: []
	FailReason sql.NullString `gorm:"column:fail_reason;type:VARCHAR;size:255;"`
	//[14] extra                                          JSONB                null: true   primary: false  isArray: false  auto: false  col: JSONB           len: -1      default: []
	Extra datatypes.JSON `gorm:"column:extra;type:JSONB;"`
	//[15] finish_time                                    TIMESTAMPTZ          null: true   primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []
	FinishTime *time.Time `gorm:"column:finish_time;type:TIMESTAMPTZ;"`
	//[16] create_time                                    TIMESTAMPTZ          null: false  primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []
	CreateTime time.Time `gorm:"column:create_time;type:TIMESTAMPTZ;"`
	//[17] update_time                                    TIMESTAMPTZ          null: false  primary: false  isArray: false  auto: false  col: TIMESTAMPTZ     len: -1      default: []
	UpdateTime time.Time `gorm:"column:update_time;type:TIMESTAMPTZ;"`
}

// TableName sets the insert table name for this struct type
func (p *Profitreturn) TableName() string {
	return "profitreturn"
}

const ReturnResultProcessing = "PROCESSING"
const ReturnResultSuccess = "SUCCESS"
const ReturnResultFailed = "FAIL"

func (p *Profitreturn) IsFinalState() bool {
	switch util.ConvertNullString(p.Result) {
	case ReturnResultSuccess, ReturnResultFailed:
		return true
	case ReturnResultProcessing:
		return false
	}
	return false
}

// 检查回退单状态是否处理中
func (p *Profitreturn) IsProcessing() bool {
	return util.ConvertNullString(p.Result) == ReturnResultProcessing
}

func (p *Profitreturn) Success(ft *time.Time) {
	p.Result = util.AdaptNullString(ReturnResultSuccess)
	p.FinishTime = ft
	p.UpdateTime = util.GetNow()
}

func (p *Profitreturn) Failed(failReason string) {
	p.Result = util.AdaptNullString(ReturnResultFailed)
	p.FailReason = util.AdaptNullString(failReason)
	p.UpdateTime = util.GetNow()
}

func (p *Profitreturn) Processing() {
	p.Result = util.AdaptNullString(ReturnResultProcessing)
	p.UpdateTime = util.GetNow()
}
